<script setup>
  import { ref,computed } from 'vue'
  import { onLoad } from '@dcloudio/uni-app'
  import taskApi from '@/apis/task.js'
  // 任务ID

  // 提货凭证照片
    const receiptPictrues = ref([])
  // 过滤掉多余的数据，只保留 url
    const certificatePictureList = computed(() => {
        return receiptPictrues.value.map(({ url }) => {
          return { url }
        })
      })
  // 提货商品照片
    const goodsPictrues = ref([])
  // 过滤掉多余的数据，只保留 url
    const deliverPictureList = computed(() => {
        return goodsPictrues.value.map(({ url }) => {
          return { url }
        })
      })
  // 2. 准备两个计算属性，来解析出[{url:''}....]
    const receiptPictruesComputed = computed(() => {
        return receiptPictrues.value.map((item) => {
          return { url: item.url }
        })
      })

    const goodsPictruesComputed = computed(() => {
        return goodsPictrues.value.map((item) => {
          return { url: item.url }
        })
      })
    // 凭证和商品都至少上传一张图片
    const enableSubmit = computed(() => {
      return goodsPictrues.value.length > 0 && receiptPictrues.value.length > 0
    })
    // 获取地址中的参数
      onLoad((query) => {
        // 任务ID
        id.value = query.value
      })
    let id = ref(0)
      onLoad((params) => {
        id = params.id
      })

      // 4. 提交数据给服务器
      async function onSubmitForm() {
        // 准备好提交的数据
        let obj = {
          id: id,
          deliverPictureList: receiptPictruesComputed.value,
          certificatePictureList: goodsPictruesComputed.value,
        }
        // 提交
        await taskApi.deliver(obj)
        uni.utils.toast('交付成功', 'success')
        uni.switchTab({
          url: '/pages/task/index',
        })
      }
</script>

<template>
  <view class="page-container">
    <view class="receipt-info">
      <uni-file-picker v-model="receiptPictrues" limit="3" title="请拍照上传回单凭证"></uni-file-picker>
      <uni-file-picker v-model="goodsPictrues" limit="3" title="请拍照上传货品照片"></uni-file-picker>
    </view>
    <!--测试获得服务器的url地址 -->

    <view class="">
      {{ receiptPictruesComputed }}
    </view>
    <view class="">
      {{ goodsPictruesComputed }}
    </view>
    <button :disabled="!enableSubmit" class="button" @click="onSubmitForm()">提交</button>
  </view>
</template>



<style lang="scss" scoped>
  .page-container {
    padding: 30rpx;
  }

  .receipt-info {
    min-height: 600rpx;
    background-color: #fff;
    padding: 20rpx 30rpx;
    border-radius: 16rpx;

    ::v-deep .uni-file-picker {
      margin-bottom: 30rpx;
    }
  }

  .button {
    height: 100rpx;
    text-align: center;
    line-height: 100rpx;
    /* #ifdef APP */
    padding-top: 4rpx;
    /* #endif */
    border-radius: 100rpx;
    margin-top: 60rpx;
    color: #fff;
    font-size: $uni-font-size-big;
    background-color: $uni-primary;

    &[disabled] {
      color: #fff;
      background-color: #fadcd9;
    }
  }
</style>
